﻿using CommDll;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebServiceComm.Model;

namespace HospitalMedicineTraceability
{
    class DataDeal
    {

        /// <summary>
        /// 右键双击设置标题信息 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;            
            // 右键双击设置标题信息 
            if (e.Button == MouseButtons.Right)
            {
                string strDgvName = (dgv.Tag == null) ? "" : dgv.Tag.ToString();
                if (strDgvName.Equals(""))
                {
                    CommFun._messageBoxShow("数据表格未定义名称！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                frmDataShowSetup frm = new frmDataShowSetup();
                frm.m_dgvMain = dgv;
                frm.m_strInfoKind = strDgvName;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.m_bIsSaveWidth)
                    {
                        // 保存宽度
                        AppConfig._saveDgvWidth(dgv, strDgvName, true);
                        CommFun._messageBoxShow("显示宽度保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        // 刷新数据
                        if (dgv.Rows.Count > 0)
                        {
                            DataTable dt = ((DataTable)dgv.DataSource).Copy();
                            // 0101 调整字段顺序
                            CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[strDgvName]);
                            // 0102 显示数据
                            CommFun._showDataGridView(dgv, dt, AppConfig.m_dictFieldWidth[strDgvName], AppConfig.m_dictFieldName[strDgvName], dgv.Columns.Contains("IsSelected"));
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 保存位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void splitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer sc = (SplitContainer)sender;
            int nHeight = sc.SplitterDistance;
            AppConfig.m_iniSetup._writeValue("Common", sc.Tag.ToString(), nHeight.ToString());  
        }

        /// <summary>
        /// // 防止闪烁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void splitContainer_SizeChanged(object sender, EventArgs e)
        {
            ((SplitContainer)sender).Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
        }

        /// <summary>
        /// 取库房信息
        /// </summary>
        public static void _getWarehouse() {
            string strSql = "select Name +'|'+ Code as Name from TH_Warehouse";
            AppInfo.m_dtWarehouse = AppInfo._openSql_DataTable(strSql);
        }
      
        /// <summary>
        /// 取功能名称信息
        /// </summary>
        public static string _getLimitName(string strLimit)
        {
            List<String> listLimit= new List<string>(strLimit.Split(','));
            string strReturn = "";
            foreach (DataRow dr in AppInfo.m_dtFunction.Rows) {
                if (listLimit.Contains(dr["Code"].ToString())) { 
                    strReturn += (strReturn.Equals("")?"":",") + dr["Name"].ToString();
                }
            }
            return strReturn;
        }

        /// <summary>
        /// 取角色名称信息
        /// </summary>
        public static string _getRoleName(string strInfo)
        {
            List<String> listInfo = new List<string>(strInfo.Split(','));
            string strReturn = "";
            string strSql = "select Code,Name from TH_System_Role order by Code";
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            foreach (DataRow dr in dt.Rows)
            {
                if (listInfo.Contains(dr["Code"].ToString()))
                {
                    strReturn += (strReturn.Equals("") ? "" : ",") + dr["Name"].ToString();
                }
            }
            return strReturn;
        }
    }
}
